package winter_vacation_homework.day_13;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 【LeetCode题号: 917. 仅仅反转字母】
 * https://leetcode-cn.com/problems/reverse-only-letters/
 */
public class ReverseOnlyLetters {
    public String reverseOnlyLetters(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Integer , Character> map = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (((int)chars[i] <= 90 && (int)chars[i] >= 65) || ((int)chars[i] >= 97 && (int)chars[i] <= 122)) {
                stack.push(chars[i]);
            }else {
                map.put(i , chars[i]);
            }
        }
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(i)){
                chars[i] = map.get(i);
            }else {
                chars[i] = stack.pop();
            }
        }
        return new String(chars);

    }
}
